package constant.milk.toilet.broadcast;



import constant.milk.toilet.IntroActivity;
import constant.milk.toilet.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

public class PushBroadcast extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		// 설정된 알림사운드를 가져와서 재생할 수 있습니다.
		Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);

		// 노티바에 뛰우기
		NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int pushAlarm = p.getInt(context.getString(R.string.prefPushAlarm), 1);

		String title = "";
		String message = "";
		long milliseconds = System.currentTimeMillis();
		
		if (pushAlarm == 1) // 1일차
		{
			title = "무소식 " + pushAlarm + "일차";
			message = "신호가 오면 바로 화장실로 출발!";
			milliseconds = milliseconds + (2 * 24 * 60 * 60000);
			pushAlarm = 2;
		}
		else if (pushAlarm == 2) // 2일차
		{
			title = "무소식 " + pushAlarm + "일차";
			message = "물을 많이 마시는 것이 쾌변의 지름길!";
			milliseconds = milliseconds + (4 * 24 * 60 * 60000);
			pushAlarm = 4;
		}
		else if (pushAlarm == 4) // 4일차
		{
			title = "무소식 " + pushAlarm + "일차";
			message = "아직도 못가셨어요? ㅜㅜ 조금만 더 힘내요!";
			milliseconds = milliseconds + (7 * 24 * 60 * 60000);
			pushAlarm = 7;
		}
		else // 1주일이상
		{
			title = "무소식 " + pushAlarm + "일차";
			message = "위험해요! 유용한 팁에 있는 음식들을 먹어 보아요.";
			milliseconds = milliseconds + (7 * 24 * 60 * 60000);
			pushAlarm = pushAlarm + 7;
		}
		
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

		builder.setSmallIcon(R.drawable.sticker_2);
		builder.setContentTitle(title);
		builder.setContentText(message);
		builder.setAutoCancel(true); // 알림바에서 자동 삭제
		builder.setSound(ringtoneUri);

		Intent intent2 = new Intent(context, IntroActivity.class);
		PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(),
				(int) System.currentTimeMillis(), intent2, PendingIntent.FLAG_CANCEL_CURRENT);
		builder.setContentIntent(pIntent);

		nm.notify(pushAlarm, builder.build());
		
		SharedPreferences.Editor e = p.edit();
		e.putInt(context.getString(R.string.prefPushAlarm), pushAlarm);
		e.commit();
		
		// 알람을 종료하고 다시 실행한다.
		Intent intent3 = new Intent(context, PushBroadcast.class);    
		PendingIntent sender = PendingIntent.getBroadcast(context, 1, intent3, 
				PendingIntent.FLAG_CANCEL_CURRENT);
		
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.cancel(sender);
        
        int version = Build.VERSION.SDK_INT;
		
		if (version >= 19)
		{
			manager.setExact(AlarmManager.RTC_WAKEUP, milliseconds, sender);
		}
		else
		{
			manager.set(AlarmManager.RTC_WAKEUP, milliseconds, sender);
		}
	}

}
